package com.sskj.one.proxy;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.constraint.Group;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.method.SingleLineTransformationMethod;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.HideUtil;
import com.sskj.one.R;
import com.sskj.one.bean.ProxyBean;
import com.sskj.one.component.DaggerUserDataComponent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/05/21
 */
public class ProxyFragment extends BaseFragment<ProxyPresenter> {

    @BindView(R.id.iv_head)
    ImageView iv_head;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_mobile)
    TextView tv_mobile;
    @BindView(R.id.smartRefreshView)
    SmartRefreshLayout smartRefreshView;
    @BindView(R.id.tv_today_commission)
    TextView tvTodayCommission;
    @BindView(R.id.tv_team_num)
    TextView tvTeamNum;
    @BindView(R.id.iv_see_money)
    ImageView iv_see_money;
    @BindView(R.id.tv_commission)
    TextView tvCommission;
    @BindView(R.id.tv_commission_unsee)
    TextView tv_commission_unsee;
    @BindView(R.id.tv_today_commission_unsee)
    TextView tv_today_commission_unsee;
    @BindView(R.id.tv_team_num_unsee)
    TextView tv_team_num_unsee;
    @BindView(R.id.tv_today_commission1_unsee)
    TextView tv_today_commission1_unsee;
    @BindView(R.id.tv_today_team_unsee)
    TextView tv_today_team_unsee;
    @BindView(R.id.tv_team_level)
    TextView tvTeamLevel;
    @BindView(R.id.tv_today_commission1)
    TextView tvTodayCommission1;
    @BindView(R.id.tv_today_team)
    TextView tvTodayTeam;
    @BindView(R.id.tv_my_level)
    TextView tvMyLevel;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;
    @BindView(R.id.group)
    Group group;
    @BindView(R.id.tv_user_mobile)
    TextView tvUserMobile;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;

    private BaseAdapter<ProxyBean.LaddersBean> adapter;


    private int page = 1;
    private int size = 20;
    private boolean ifSee;
    private HashMap<String, Integer> map;


    @Override
    public int getLayoutId() {
        return R.layout.app_fragment_proxy;
    }

    @Override
    public ProxyPresenter getPresenter() {
        return new ProxyPresenter();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            if (scrollView != null) {
                scrollView.fling(0);
                scrollView.smoothScrollTo(0, 0);
            }
            if (mPresenter != null) {
                page = 1;
                mPresenter.getProxyInfo(false, page, size);
            }
        }
    }

    @Override
    public void initView() {
        DaggerUserDataComponent.create().inject(this);
        map = new HashMap<>();
        map.put("1", R.mipmap.head_one);
        map.put("2", R.mipmap.head_two);
        map.put("3", R.mipmap.head_three);
        map.put("4", R.mipmap.head_four);
        map.put("5", R.mipmap.head_five);
        map.put("6", R.mipmap.head_six);
        map.put("7", R.mipmap.head_seven);
        map.put("8", R.mipmap.head_eight);
        map.put("9", R.mipmap.head_nine);
        map.put("10", R.mipmap.head_ten);
        map.put("11", R.mipmap.head_eleven);
        map.put("12", R.mipmap.head_twelve);
        map.put("13", R.mipmap.head_thirteen);
        map.put("14", R.mipmap.head_fourteen);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                if (isEmpty(userData.getUpic()) || isEmpty(userData.getNickname())) {
                    group.setVisibility(View.GONE);
                    tvUserMobile.setVisibility(View.VISIBLE);
                } else {
                    group.setVisibility(View.VISIBLE);
                    tvUserMobile.setVisibility(View.GONE);
                    ImageUtil.setImage(map.get(userData.getUpic()), iv_head);
                }
                tvUserMobile.setText(HideUtil.getPhoneHide(userData.getMobile()));
                tvName.setText(userData.getNickname());
                tv_mobile.setText(HideUtil.getPhoneHide(userData.getMobile()));
            } else {
                userData = null;
            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(App.INSTANCE));
        adapter = new BaseAdapter<ProxyBean.LaddersBean>(R.layout.item_rank, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, ProxyBean.LaddersBean item) {
                holder.setText(R.id.tv_name, item.getNickname())
                        .setText(R.id.tv_mobile, item.getMobile())
                        .setVisible(R.id.ll_user, !isEmpty(item.getUpic()) && !isEmpty(item.getNickname()))
                        .setVisible(R.id.tv_mobile, isEmpty(item.getUpic()) || isEmpty(item.getNickname()))
                        .setImageResource(R.id.iv_head, isEmpty(item.getUpic()) ? R.mipmap.app_logo : map.get(item.getUpic()))
                        .setText(R.id.tv_date, item.getAddtime())
                        .setText(R.id.tv_number, (holder.getAdapterPosition() + 1) > 9 ? "" + (holder.getAdapterPosition() + 1) : "0" + (holder.getAdapterPosition() + 1))
                        .setText(R.id.tv_fenhong, item.getMoney_display() == 0 ? "******" : item.getFee_yongjin());
            }
        };
        ClickUtil.click(300, tvTeamLevel, () -> {
            TeamLevelActivity.start(getActivity());
        });
    }

    @Override
    public void initData() {
        mPresenter.getProxyInfo(true, page, size);
        smartRefreshView.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getProxyInfo(false, page, size);
        });
        smartRefreshView.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getProxyInfo(false, page, size);
        });

        ClickUtil.click(300, iv_see_money, () -> {
            if (!ifSee) {
                iv_see_money.setImageResource(R.mipmap.icon_proxy_unsee);
                tv_commission_unsee.setVisibility(View.VISIBLE);
                tv_today_commission_unsee.setVisibility(View.VISIBLE);
                tv_team_num_unsee.setVisibility(View.VISIBLE);
                tv_today_commission1_unsee.setVisibility(View.VISIBLE);
                tv_today_team_unsee.setVisibility(View.VISIBLE);
                tvCommission.setVisibility(View.GONE);
                tvTodayCommission1.setVisibility(View.GONE);
                tvTodayCommission.setVisibility(View.GONE);
                tvTeamNum.setVisibility(View.GONE);
                tvTodayTeam.setVisibility(View.GONE);
                ifSee = true;
            } else {
                iv_see_money.setImageResource(R.mipmap.icon_proxy_see);
                tv_commission_unsee.setVisibility(View.GONE);
                tv_today_commission_unsee.setVisibility(View.GONE);
                tv_team_num_unsee.setVisibility(View.GONE);
                tv_today_commission1_unsee.setVisibility(View.GONE);
                tv_today_team_unsee.setVisibility(View.GONE);
                tvCommission.setVisibility(View.VISIBLE);
                tvTodayCommission1.setVisibility(View.VISIBLE);
                tvTodayCommission.setVisibility(View.VISIBLE);
                tvTeamNum.setVisibility(View.VISIBLE);
                tvTodayTeam.setVisibility(View.VISIBLE);
                ifSee = false;
            }
        });
    }

    public void stopRefresh() {
        smartRefreshView.finishRefresh();
        smartRefreshView.finishLoadMore();
    }

    public static ProxyFragment newInstance() {
        ProxyFragment fragment = new ProxyFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @SuppressLint("SetTextI18n")
    public void getProxyBeanOK(ProxyBean data) {
        tvMyLevel.setText(data.getPaiming());
        tvCommission.setText("$ " + NumberUtil.keep(data.getTotal_assets(), 5));
        tvTodayCommission.setText("$" + NumberUtil.keep(data.getToday_assets(), 5));
        tvTodayCommission1.setText("+$" + NumberUtil.keep(data.getToday_assets(), 5));
        tvTeamNum.setText(data.getTotal_team_number() + "人");
        tvTodayTeam.setText(data.getToday_team_number() + "人");
        if (adapter != null && data.getLadders() != null) {
            if (page == 1) {
                adapter.setNewData(data.getLadders());
            } else {
                adapter.addData(data.getLadders());
            }
        }
    }
}
